﻿var lsInfo = {
  current: 1,
  pageSize: allPageSize,
  data: [],
  totalRow: 0,
  totalPage: 0,
  oneDate: {},
  editState: false
};

// document.getElementById('nav').innerHTML = template('navs', navinfo4);

$('.indexl43').addClass('active');

// monthEvent();
// function monthEvent() {
// console.log(y, m);
var month = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
var now = new Date();
var y = now.getFullYear();
var m = now.getMonth();
var d = now.getDate();
$('#month').val(month[m]);
$('#prevMonth').click(function () {
  m--;
  m = checkMonth(y, m).m;
  y = checkMonth(y, m).y;
  $('#month').val(month[m]);
  showPage(lsInfo);
});
$('#nextMonth').click(function () {
  m++;
  m = checkMonth(y, m).m;
  y = checkMonth(y, m).y;
  $('#month').val(month[m]);
  showPage(lsInfo);
});

// }

function checkMonth(y, m) {
  if (m < 0) {
    m = 11;
    y = y - 1;
  } else if (m > 11) {
    m = 0;
    y = y + 1;
  }
  return {
    y: y,
    m: m
  }
}


var getList = function (obj) {
  // console.log(y, m)
  if (m === 0) {
    y = y - 1;
  }
  $.ajax({
    cache: false,
    async: false,
    url: '/api/CommonApi?API=GetTeacherMonthApplys',
    type: 'get',
    data: {
      sorts: ' StartDate DESC',
      Year: y,
      Month: m,
      pagesize: obj.size,
      page: obj.index
    },
    dataType: 'json',
    success: function (data) {
      // console.log(data)
      if (data.Success) {
        obj.callback(data)
      }
    }
  });
};

//load();
//function load() {
  getList({
    size: lsInfo.pageSize,
    index: lsInfo.current,
    callback: function (data) {
      // console.log(data)
      // console.log(JSON.stringify(data));
      var newData = data.ApiParamObj[0];
      for (var key in newData) {
        newData[key].StartDate = new Date(newData[key].StartDate).pattern('yyyy/MM/dd');
        newData[key].EndDate = new Date(newData[key].EndDate).pattern('yyyy/MM/dd');
        if (newData[key]['State'] === '????') {
          newData[key]['State'] = '';
        }
      }
      // 列表数据
      document.getElementById('listTable').innerHTML = template('l43List', newData);
      // 分页
      var pagesInfo = data.ApiParamObj[1];
      lsInfo.data = newData;
      lsInfo.totalRow = pagesInfo[0].totalRow;
      lsInfo.totalPage = pagesInfo[0].totalPage;

      $('#totalRow').html(lsInfo.totalRow);
      pagesSet(lsInfo);
      setPageSize(lsInfo);
      loading.hide();
    }
  });
//}

// 点击分页请求事件
var showPage = function (o) {
  // console.log(o)
  getList({
    size: o.pageSize,
    index: o.current,
    callback: function (data) {
      // console.log(data)
      // console.log(JSON.stringify(data));
      var newData = data.ApiParamObj[0];
      for (var key in newData) {
        newData[key].StartDate = new Date(newData[key].StartDate).pattern('yyyy/MM/dd');
        newData[key].EndDate = new Date(newData[key].EndDate).pattern('yyyy/MM/dd');
        if (newData[key]['State'] === '????') {
          newData[key]['State'] = '';
        }
      }
      // 列表数据
      document.getElementById('listTable').innerHTML = template('l43List', newData);
      // 分页
      var pagesInfo = data.ApiParamObj[1];
      o.data = newData;
      o.totalRow = pagesInfo[0].totalRow;
      o.totalPage = pagesInfo[0].totalPage;

      $('#totalRow').html(o.totalRow);
      pagesSet(o);
      loading.hide();
    }
  });
};

// 预览报销单
$('#table').on('click', '.prview', function () {
  var index = $(this).parent().parent().index();
  lsInfo.oneDate = lsInfo.data[index];
  var v = lsInfo.oneDate;
  var date = new Date(v.StartDate).pattern('yyyy-MM-dd#HH-mm-ss');
  // console.log(v)
  // window.location.href = '/Finance/Project/print/' + v.MonthApplyID + '=' + date;
  // window.open('/Teacher/Project/print4/' + v.MonthApplyID + '=' + date, '_blank');
  var url = '/Teacher/Project/print4/' + v.MonthApplyID + '=' + date;
  window.location.href = url;
  // $('#editCost').modal('show');
  return false;
});

// 提交报销单
$('#table').on('click', '.submit', function () {
  // 每个月15前号才能进行报销
  /*if (d >= 15) {
    alert('请在每月15日前提交上月报销单，每个项目报销限提报1次/月!');
    return false;
  }*/

  var index = $(this).parent().parent().index();
  lsInfo.oneDate = lsInfo.data[index];
  var v = lsInfo.oneDate;
  var submitObj = {
    Role: 'TEACHER',
    TeacherMonthApplyID: v.MonthApplyID,
    State: '1_SUBMIT',
    Explain: ''
  };
  // return false;
  $.ajax({
    url: '/api/CommonApi?API=TeacherMonthApplyAuditByRole',
    type: 'get',
    data: submitObj,
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        console.log(data);
        showPage(lsInfo);
      }
    }
  });
  // $('#editCost').modal('show');
});

// 提交报销单
$('#table').on('click', '.sent', function () {
  var index = $(this).parent().parent().index();
  lsInfo.oneDate = lsInfo.data[index];
  $('#editCost').modal('show');
});

$('#editCost').on('click', '#btn-save', function () {
  var v = lsInfo.oneDate;
  var v1 = $('#td1 :selected').val();
  var v2 = $('#td2').val();

  if (!v2) {
    alert('快递d单号不能为空！');
    return false;
  }

  var submitObj = {
    Role: 'TEACHER',
    TeacherMonthApplyID: v.MonthApplyID,
    State: '4_TEACHER_COURIER',
    Explain: 'v1:v2'
  };
  $.ajax({
    url: '/api/CommonApi?API=TeacherMonthApplyAuditByRole',
    type: 'get',
    data: submitObj,
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        console.log(data);
        showPage(lsInfo);
      }
    }
  });
  $('#editCost').modal('hide');
});

// 查看进度
$('#table').on('click', '.lookStep', function () {
  var index = $(this).parent().parent().index();
  lsInfo.oneDate = lsInfo.data[index];
  var v = lsInfo.oneDate;
  // console.log(v)
  $.ajax({
    url: '/api/CommonApi?API=GetTeacherMonthApplyLog',
    type: 'get',
    data: {
      TeacherMonthApplyID: v.MonthApplyID
    },
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        // console.log(data)
        // console.log(JSON.stringify(data));
        // console.log()
        // var obj = {};
        // obj.ProjectName = v.ProjectName;
        // obj.TypeName = v.TypeName;
        // obj.BrandName = v.BrandName;
        // obj.StageName = v.StageName;
        // obj.SubmitTime = v.SubmitTime;
        // obj.UserName = v.UserName;
        // obj.data = data.ApiParamObj;
        // data.ApiParamObj = unique(data.ApiParamObj);
        document.getElementById('viewDetail').innerHTML = template('detail', data);
      }
    }
  });
  $('#postCost').modal('show');
});


function unique(arr) {
  var result = [];
  var key = {};
  var i = 0;
  var l = arr.length;
  for ( ; i < l ;i++) {
    // console.log(arr[i])
    if (!key[arr[i].TriggerMan]) {
      key[arr[i].TriggerMan] = true;
      result.push(arr[i]);
    }
  }
  return result;
}